package com.legolas.config;


import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2016/11/15 0015
 * Time: 14:14
 */

/**
 * WebApplicationInitializer是Spring用来提供配置Servlet3.0+配置的接口，替代web.xml的位置，
 * 实现此接口，将会自动被SpringServletContainerInilitializer（用来启动servlet3.0+容器）获取。
 */

public class WebInitializer implements WebApplicationInitializer{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException{
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MyMvcConfig.class);

        //新建WebApplicationContext，注册配置类，并将其和当前servletContext关联
        ctx.setServletContext(servletContext);

        //注册Spring Mvc的DispatcherServlet
        Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}
